package com.mzx.javase.yichang;

/**
 * @Description:抛出自定义的异常
 * @Date: 2022/3/1 10:12
 * @Author: MarKing
 */
public class Test3 {

    public static void main(String[] args) {
        Person person = new Person();
        person.setAge(-2);
        System.out.println("person初始化成功");
        System.out.println(person);
    }
}
class Person{
    private int age;
    private String name;

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age < 0) {
            try {
                throw new myException("输入的年龄不符合规范");
            } catch (myException e) {
                e.printStackTrace();
            }
        }else {
            this.age = age;
        }
    }

}
